package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.BusinessQualification;
import com.weitu.contractor.entity.BusinessQualificationCriteria;
import com.weitu.contractor.mapper.BusinessQualificationMapper;
import com.weitu.contractor.service.BusinessQualificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessQualificationServiceImpl implements BusinessQualificationService {
    private final BusinessQualificationMapper businessQualificationMapper;

    private static final Logger logger = LoggerFactory.getLogger(BusinessQualificationServiceImpl.class);

    @Autowired
    public BusinessQualificationServiceImpl(BusinessQualificationMapper businessQualificationMapper) {
        this.businessQualificationMapper = businessQualificationMapper;
    }

    @Override
    public int countByCriteria(BusinessQualificationCriteria example) {
        int count = this.businessQualificationMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public BusinessQualification selectByPrimaryKey(String id) {
        return this.businessQualificationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BusinessQualification> selectByCriteria(BusinessQualificationCriteria example) {
        return this.businessQualificationMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.businessQualificationMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(BusinessQualification record) {
        return this.businessQualificationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(BusinessQualification record, BusinessQualificationCriteria example) {
        return this.businessQualificationMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(BusinessQualification record) {
        return this.businessQualificationMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorId, String name, String certCode) {
        BusinessQualificationCriteria criteria = new BusinessQualificationCriteria();
        criteria.or().andNameEqualTo(name).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0);
        criteria.or().andCertCodeEqualTo(certCode).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0);
        return businessQualificationMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public boolean checkIfExtraExist(String contractorId, String name, String certCode, String id) {
        BusinessQualificationCriteria criteria = new BusinessQualificationCriteria();
        criteria.or().andNameEqualTo(name).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0).andIdNotEqualTo(id);
        criteria.or().andCertCodeEqualTo(certCode).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0).andIdNotEqualTo(id);
        return businessQualificationMapper.countByCriteria(criteria) > 0;
    }
}